package com.example.demo.thread.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Description: CountDownLatch 例子
 * @Author: LLL
 * @Date: 2020-03-19 17:51
 **/
public class CountDownLatchDemo {
    private static CountDownLatch latch = new CountDownLatch(7);

    public static void main(String[] args) throws InterruptedException {

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "\t上完自习离开教室..");
                latch.countDown();
            }, String.valueOf(i)).start();
        }
        Thread.sleep(1000);
        latch.countDown();
        //        latch.await(3, TimeUnit.SECONDS);
        print();

    }

    public static void print() throws InterruptedException {
        latch.await();
        System.out.println(Thread.currentThread().getName() + "\t班长锁门，离开教室！");
    }
}
